import { defineStore } from 'pinia'
import router from '@/router/index'

let LocalMenuConfig=JSON.parse(localStorage.getItem("MenuConfig")||"{}")


const useSysStore = defineStore('sysStore', {
  state: () => ({ 
       MenuConfig:{
           _selectedMenuIds:LocalMenuConfig?._selectedMenuIds||[] as Array<string>, //保存当前激活的菜单项的id
           _openMenuIds:LocalMenuConfig?._openMenuIds||[] as Array<string>,//保存当前展开的菜单项的id
           _curMenuTree:LocalMenuConfig?._curMenuTree||{} as Array<UserType.MenuTreeInfo>, //保存当前激活的菜单项所处的整个菜单树结构
       }
         
  }),
  getters: {
    selectedMenuIds():Array<string>{
        return this.MenuConfig._selectedMenuIds
    },
    openMenuIds():Array<string>{
        return this.MenuConfig._openMenuIds
    },
    curMenuTree():UserType.MenuTreeInfo{
        return this.MenuConfig._curMenuTree
    }
  },
  actions: {
      saveSelectedMenuIds(menuIds:Array<string>){
          this.MenuConfig._selectedMenuIds = menuIds
          localStorage.setItem('MenuConfig',JSON.stringify(this.MenuConfig))
      },
      saveOpenMenuIds(menuIds:Array<string>){
          this.MenuConfig._openMenuIds = menuIds
          localStorage.setItem('MenuConfig',JSON.stringify(this.MenuConfig)) 
      },
      saveCurMenuTreeList(MenuTree:UserType.MenuTreeInfo){
          this.MenuConfig._curMenuTree = MenuTree
          localStorage.setItem('MenuConfig',JSON.stringify(this.MenuConfig)) 
      },
      removeMenuConfig(){
  
          this.MenuConfig = {
              _selectedMenuIds:[],
              _openMenuIds:[],
              _curMenuTree:{}
          } 
          localStorage.removeItem('MenuConfig')
      }
    },
})

export default useSysStore